<template>
  <div class="login-container">
    <div class="login-box">
      <div class="logo">
        <h1>职途导师</h1>
        <p class="slogan">提升面试能力 · 助力职业发展</p>
      </div>
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" @submit.prevent="handleLogin">
        <el-form-item prop="account">
          <el-input v-model="loginForm.account" placeholder="学号/用户名">
            <template #prefix>
              <el-icon><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password" placeholder="密码" show-password>
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <div class="login-options">
          <el-checkbox v-model="loginForm.rememberMe">记住我</el-checkbox>
          <a href="#" class="forgot-password">忘记密码?</a>
        </div>
        <el-form-item>
          <el-button type="primary" :loading="loading" class="login-button" @click="handleLogin">登录</el-button>
        </el-form-item>
      </el-form>
      <div class="register-prompt">
        <span>没有账号?</span>
        <a href="#">立即注册</a>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import { User, Lock } from '@element-plus/icons-vue';
import type { FormInstance } from 'element-plus';
import { login } from '@/api/user';
import type { LoginParams } from '@/api/user';

const router = useRouter();
const loginFormRef = ref<FormInstance | null>(null);
const loading = ref(false);
const loginForm = reactive<LoginParams>({
  account: '',
  password: '',
  rememberMe: false
});

const loginRules = reactive({
  account: [
    { required: true, message: '请输入学号/用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ]
});

const handleLogin = async () => {
  if (!loginFormRef.value) return;
  
  loginFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true;
      console.log('Login request', {
        account: loginForm.account,
        password: loginForm.password,
        rememberMe: loginForm.rememberMe
      });
      const response = await login(loginForm);
      if(response.code===1){
        // 登录成功提示
        ElMessage({
          type: 'success',
          message: '登录成功'
        });
        localStorage.setItem('token', response.token)
        localStorage.setItem('isLogin', 'true')
        
        localStorage.setItem('userid',response.user.id);
        localStorage.setItem('username',response.user.user_name);
        // 跳转到首页
        router.push('/home');
      }else{
        ElMessage({
          type: 'error',
          message: '请填写正确的登录信息'
        });
        return false;
      }
    } else {
      ElMessage({
        type: 'error',
        message: '请填写正确的登录信息'
      });
      return false;
    }
  });
};
</script>

<style scoped>
.login-container {
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-image: url('https://img.51miz.com/Element/00/71/48/24/3a6c8a7e_E714824_95a78c9c.jpg');
  background-size: cover;
  background-position: center;
}

.login-box {
  width: 400px;
  padding: 40px;
  background-color: rgba(255, 255, 255, 0.9);
  border-radius: 10px;
  box-shadow: 0 15px 30px rgba(0, 0, 0, 0.1);
}

.logo {
  text-align: center;
  margin-bottom: 30px;
}

.logo h1 {
  margin: 0;
  font-size: 28px;
  color: #1890ff;
}

.slogan {
  margin-top: 10px;
  font-size: 14px;
  color: #888;
}

.login-options {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.forgot-password {
  color: #1890ff;
  text-decoration: none;
}

.login-button {
  width: 100%;
  height: 44px;
  font-size: 16px;
}

.register-prompt {
  text-align: center;
  margin-top: 20px;
  font-size: 14px;
}

.register-prompt a {
  color: #1890ff;
  text-decoration: none;
  margin-left: 5px;
}

:deep(.el-input__inner) {
  height: 44px;
}
</style>